try:
   scriptRoot = os.environ['BLENDER_ROBOTICS_ROOT']
except:
   scriptRoot = '/home/slemaign/work/simulation/simu_blender/scripts'
print "scriptRoot:", scriptRoot

import sys
sys.path.append(scriptRoot) 

import Rasterizer, Blender, GameLogic

from middleware.yarp import YarpBlender

print '######## SIMULATOR INITIALIZATION ########'
print

#YARP initialization
YarpBlender.initialize(['/blender_simu/cam'], ['/blender_simu/cog_map'])

#Display mouse cursor
Rasterizer.showMouse(1)

# get game window height and width
height = Rasterizer.getWindowHeight()
width = Rasterizer.getWindowWidth()

# get the active camera
scene = GameLogic.getCurrentScene()

# get the active controller
cont = GameLogic.getCurrentController()

# get cameras from object list
camUser = scene.getObjectList()["OBCameraUser"]
camRobot = scene.getObjectList()["OBCameraRobot"]

#get a reference to a scene actuator, needed to change the active camera
sceneCamera = cont.getActuator('sceneCamera')


######## Set viewports ##########

# camUser viewport: entire screen
left_A = 0
bottom_A = 0
right_A = width
top_A = height

camUser.setViewport( left_A, bottom_A, right_A, top_A)

#  camRobot viewport: corner top right
left_B = width * 2 / 3
bottom_B = height * 2 / 3
right_B = width
top_B = height
           
camRobot.setViewport( left_B, bottom_B, right_B, top_B) 
camRobot.setOnTop()

#Stores the position of the robot's camera in the OpenGL window for later image grabbing ([bottom left corner X,bottom left corner Y, width, height])
GameLogic.camRobotViewport = [left_B, bottom_B, right_B - left_B , top_B - bottom_B]

#Important: activate the viewport (else, nothing happen!)
camUser.enableViewport(True)
camRobot.enableViewport(True)

# Set the active camera
sceneCamera.setCamera(camUser)
GameLogic.addActiveActuator(sceneCamera, True)

########## Create and store global variables ###########

#If they were not yet created, we initialize them as member of the global GameLogic object.

#trackedObject is a dictionary containing the list of tracked objects (->meshes with a class property set up) as keys and the bounding boxes of these objects as value.
if not hasattr(GameLogic, 'trackedObjects'):
	print ' * Initialization of trackedObjects variable...'
	GameLogic.trackedObjects = dict.fromkeys([ obj for obj in scene.getObjectList() if obj.getPropertyNames().count('objClass')!=0 ])
	for obj in GameLogic.trackedObjects.keys():
		GameLogic.trackedObjects[obj] = Blender.Object.Get(obj.name[2:]).getBoundBox(0) #getBoundBox(0) returns the bounding box in local space instead of world space.
		print '  - ',obj.name

#visibleObjects stores the list of currently visible objects by the robot.
if not hasattr(GameLogic, 'visibleObjects'):
	print ' * Initialization of visibleObjects variable...'
	GameLogic.visibleObjects = []

#selectedObject stores the current selected object
if not hasattr(GameLogic, 'selectedObject'):
	print ' * Initialization of selectedObject variable...'
	GameLogic.selectedObject = None

#ugly hack to solve the double triggering in object_selection.py that occur when an object is selected.
if not hasattr(GameLogic, 'ignoreFlag'):
	GameLogic.ignoreFlag = False
	
print
print '######## END OF INITIALIZATION ########'
print
